package com.hanter.android.radlib.countdown.impl;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hanter.android.radlib.countdown.CountDownTimerService;
import com.hanter.android.radlib.countdown.MyCountDownTimer;
import com.hanter.android.radlib.countdown.exception.TimerOutOfDateException;

public class SqliteCdtService implements CountDownTimerService {

    private TimerSqlOpenHelper sqlOpenHelper;

    public SqliteCdtService(Context context) {
        this.sqlOpenHelper = new TimerSqlOpenHelper(context.getApplicationContext());
    }

    @Override
    public MyCountDownTimer createTimer(String name, String group, long millisInFuture, long interval) {
        long startTime = System.currentTimeMillis();
        long endTime = startTime + millisInFuture;
        SQLiteDatabase db = null;
        try {
            db = sqlOpenHelper.getWritableDatabase();
            db.execSQL("INSERT INTO " + TimerSqlOpenHelper.TABLE_TIMERS + "  VALUES ("
                    + "'" + name + "',"
                    + "'" + group + "',"
                    + interval + ","
                    + startTime + ","
                    + endTime + ","
                    + "'start'" + ")");

            return newTimer(name, group, interval, startTime, endTime);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }

    @Override
    public void deleteTimer(String name, String group) {
        SQLiteDatabase db = sqlOpenHelper.getWritableDatabase();
        db.delete(TimerSqlOpenHelper.TABLE_TIMERS,
                TimerSqlOpenHelper.COLUMN_TIMER_NAME + " = ? AND " + TimerSqlOpenHelper.COLUMN_TIMER_GROUP + " = ?",
                new String[]{name, group});
        db.close();
    }

    @Override
    public MyCountDownTimer getTimer(String name, String group) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = sqlOpenHelper.getWritableDatabase();
            if (db == null)
                return null;

            cursor = db.query(TimerSqlOpenHelper.TABLE_TIMERS,
                    null,
                    TimerSqlOpenHelper.COLUMN_TIMER_NAME + " = ? AND " + TimerSqlOpenHelper.COLUMN_TIMER_GROUP + " = ?",
                    new String[]{name, group},
                    null,
                    null,
                    null);

            cursor.moveToFirst();
            long interval = cursor.getLong(cursor.getColumnIndexOrThrow(TimerSqlOpenHelper.COLUMN_INTERVAL));
            long startTime = cursor.getLong(cursor.getColumnIndexOrThrow(TimerSqlOpenHelper.COLUMN_START_TIME));
            long finishTime = cursor.getLong(cursor.getColumnIndexOrThrow(TimerSqlOpenHelper.COLUMN_FINISH_TIME));
            return newTimer(name, group, interval, startTime, finishTime);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (cursor != null) {
                cursor.close();
            }

            if (db != null) {
                db.close();
            }
        }
    }

    private MyCountDownTimer newTimer(String name, String group, Long interval, Long startTime, Long finishTime) throws TimerOutOfDateException {
        long currentTime = System.currentTimeMillis();
        long millisInFuture = finishTime - currentTime;

        if (millisInFuture <= 0) {
            // TODO 重置状态或删除
            throw new TimerOutOfDateException();
        }

        if (interval <= 0)
            interval = 1000L;

        return new MyCountDownTimer(millisInFuture, interval);
    }

}
